package cn.xueden.security;

import cn.xueden.system.model.entity.User;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**功能描述：用户信息
 * @author:梁志杰
 * @date:2025/6/8
 * @description:cn.xueden.security
 * @version:1.0
 */
@Data
@NoArgsConstructor
public class SysUserDetails implements UserDetails {

    /**
     * 用户名
     */
    private String username;

    /**
     * 用户信息
     */
    private User user;

    /**
     * 权限
     */
    private List<SimpleGrantedAuthority> permissions;

    public SysUserDetails(User user) {
        this.user = user;
    }

    /**
     * 获取权限
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return permissions;
    }

    /**
     * 设置权限
     * @param permissions
     */
    public void setPermissions(List<SimpleGrantedAuthority> permissions) {
        this.permissions = permissions;
    }

    /**
     *  获取密码
     */
    @Override
    public String getPassword() {
        String myPassword = user.getPassword();
        user.setPassword("");
        return myPassword;
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
